<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item label="房间号" prop="roomNumber">
              <el-input v-model="queryParams.roomNumber" placeholder="请输入房间号" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="房间类型" prop="roomType">
              <el-select v-model="queryParams.roomType" placeholder="请选择房间类型" clearable >
                <el-option v-for="dict in sys_room_type" :key="dict.value" :label="dict.label" :value="dict.value"/>
              </el-select>
            </el-form-item>
            <el-form-item label="房间状态" prop="roomStatus">
              <el-select v-model="queryParams.roomStatus" placeholder="请选择房间状态" clearable >
                <el-option v-for="dict in sys_room_status" :key="dict.value" :label="dict.label" :value="dict.value"/>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['nursing:roomInfo:add']">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate()" v-hasPermi="['nursing:roomInfo:edit']">修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()" v-hasPermi="['nursing:roomInfo:remove']">删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['nursing:roomInfo:export']">导出</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="roomInfoList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="id" align="center" prop="roomId" v-if="true" />
        <el-table-column label="房间号" align="center" prop="roomNumber" />
        <el-table-column label="房间类型" align="center" prop="roomType">
          <template #default="scope">
            <dict-tag :options="sys_room_type" :value="scope.row.roomType"/>
          </template>
        </el-table-column>
        <el-table-column label="楼栋" align="center" prop="building" />
        <el-table-column label="楼层" align="center" prop="floor" />
        <el-table-column label="全部床位数" align="center" prop="bedCount" />
        <el-table-column label="可用床位数" align="center" prop="availableBeds" />
        <el-table-column label="价格" align="center" prop="pricePerDay" />
        <el-table-column label="设施描述" align="center" prop="facilities" />
        <el-table-column label="房间状态" align="center" prop="roomStatus">
          <template #default="scope">
            <dict-tag :options="sys_room_status" :value="scope.row.roomStatus"/>
          </template>
        </el-table-column>
        <el-table-column label="备注" align="center" prop="remarks" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-tooltip content="修改" placement="top">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['nursing:roomInfo:edit']"></el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['nursing:roomInfo:remove']"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>
    <!-- 添加或修改房间管理对话框 -->
    <el-dialog :title="dialog.title" v-model="dialog.visible" width="500px" append-to-body>
      <el-form ref="roomInfoFormRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="房间号" prop="roomNumber">
          <el-input v-model="form.roomNumber" placeholder="请输入房间号" />
        </el-form-item>
        <el-form-item label="房间类型" prop="roomType">
          <el-select v-model="form.roomType" placeholder="请选择房间类型">
            <el-option
                v-for="dict in sys_room_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="楼栋" prop="building">
          <el-input v-model="form.building" placeholder="请输入楼栋" />
        </el-form-item>
        <el-form-item label="楼层" prop="floor">
          <el-input v-model="form.floor" placeholder="请输入楼层" />
        </el-form-item>
        <el-form-item label="全部床位数" prop="bedCount">
          <el-input v-model="form.bedCount" placeholder="请输入全部床位数" />
        </el-form-item>
        <el-form-item label="可用床位数" prop="availableBeds">
          <el-input v-model="form.availableBeds" placeholder="请输入可用床位数" />
        </el-form-item>
        <el-form-item label="价格" prop="pricePerDay">
          <el-input v-model="form.pricePerDay" placeholder="请输入价格" />
        </el-form-item>
        <el-form-item label="设施描述" prop="facilities">
          <el-input v-model="form.facilities" placeholder="请输入设施描述" />
        </el-form-item>
        <el-form-item label="房间状态" prop="roomStatus">
          <el-select v-model="form.roomStatus" placeholder="请选择房间状态">
            <el-option
                v-for="dict in sys_room_status"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="RoomInfo" lang="ts">
import { listRoomInfo, getRoomInfo, delRoomInfo, addRoomInfo, updateRoomInfo } from '@/api/nursing/roomInfo';
import { RoomInfoVO, RoomInfoQuery, RoomInfoForm } from '@/api/nursing/roomInfo/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { sys_room_status, sys_room_type } = toRefs<any>(proxy?.useDict('sys_room_status', 'sys_room_type'));

const roomInfoList = ref<RoomInfoVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const roomInfoFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: RoomInfoForm = {
  roomId: undefined,
  roomNumber: undefined,
  roomType: undefined,
  building: undefined,
  floor: undefined,
  bedCount: undefined,
  availableBeds: undefined,
  pricePerDay: undefined,
  facilities: undefined,
  roomStatus: undefined,
  remarks: undefined,
}
const data = reactive<PageData<RoomInfoForm, RoomInfoQuery>>({
  form: {...initFormData},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    roomNumber: undefined,
    roomType: undefined,
    roomStatus: undefined,
    params: {
    }
  },
  rules: {
    roomId: [
      { required: true, message: "id不能为空", trigger: "blur" }
    ],
    roomNumber: [
      { required: true, message: "房间号不能为空", trigger: "blur" }
    ],
    roomType: [
      { required: true, message: "房间类型不能为空", trigger: "change" }
    ],
    roomStatus: [
      { required: true, message: "房间状态不能为空", trigger: "change" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询房间管理列表 */
const getList = async () => {
  loading.value = true;
  const res = await listRoomInfo(queryParams.value);
  roomInfoList.value = res.rows;
  total.value = res.total;
  loading.value = false;
}

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
}

/** 表单重置 */
const reset = () => {
  form.value = {...initFormData};
  roomInfoFormRef.value?.resetFields();
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
}

/** 多选框选中数据 */
const handleSelectionChange = (selection: RoomInfoVO[]) => {
  ids.value = selection.map(item => item.roomId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  dialog.visible = true;
  dialog.title = "添加房间管理";
}

/** 修改按钮操作 */
const handleUpdate = async (row?: RoomInfoVO) => {
  reset();
  const _roomId = row?.roomId || ids.value[0]
  const res = await getRoomInfo(_roomId);
  Object.assign(form.value, res.data);
  dialog.visible = true;
  dialog.title = "修改房间管理";
}

/** 提交按钮 */
const submitForm = () => {
  roomInfoFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.roomId) {
        await updateRoomInfo(form.value).finally(() =>  buttonLoading.value = false);
      } else {
        await addRoomInfo(form.value).finally(() =>  buttonLoading.value = false);
      }
      proxy?.$modal.msgSuccess("操作成功");
      dialog.visible = false;
      await getList();
    }
  });
}

/** 删除按钮操作 */
const handleDelete = async (row?: RoomInfoVO) => {
  const _roomIds = row?.roomId || ids.value;
  await proxy?.$modal.confirm('是否确认删除房间管理编号为"' + _roomIds + '"的数据项？').finally(() => loading.value = false);
  await delRoomInfo(_roomIds);
  proxy?.$modal.msgSuccess("删除成功");
  await getList();
}

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download('nursing/roomInfo/export', {
    ...queryParams.value
  }, `roomInfo_${new Date().getTime()}.xlsx`)
}

onMounted(() => {
  getList();
});
</script>
